package com.roc.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class HttpUtil {
	
	private static Logger log = LogManager.getLogger(HttpUtil.class);
	
	/**
	 * POST请求
	 * @param url 请求地址
	 * @param paramsString  参数字符串
	 * @throws Exception 
	 */
	public static void sendGetOrPost(String url, String paramsString) throws Exception {
//		log.info("请求地址："+url+"\n      请求参数："+paramsString);
		HttpURLConnection uc = null;
		PrintWriter pw = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			// 创建连接对象
			uc = (HttpURLConnection) realUrl.openConnection();

			uc.setDoOutput(true);
			uc.setDoInput(true);// 此属性默认值为true
			uc.setRequestMethod("POST");
			//POST请求不能使用缓存
			uc.setUseCaches(false);
			// 连接
			uc.connect();

			// post请求时需要此法写入请求参数，因为参数要放在http正文内，GET请求课直接将参数拼接在链接后面
			pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream(),"UTF-8"));

			pw.write(paramsString);
			pw.flush();// 需要flush，否则接口无法获取到参数
			pw.close();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			log.info("http请求返回结果："+result);
			in.close();
			
			uc.disconnect();
			log.info("成功调用Http工具");
		} catch (Exception e) {
		        log.error("发送Http请求异常:"+ e.getMessage());
			throw new Exception("发送Http请求异常");
			
			
		} 
	}
	
	/**
	 * 有返回值的
	 * @param url
	 * @param paramsString
	 * @return
	 * @throws Exception
	 */
	public static String sendGetOrPostR(String url, String paramsString) throws Exception {
		log.info("请求地址："+url+"\n      请求参数："+paramsString);
		HttpURLConnection uc = null;
		PrintWriter pw = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			// 创建连接对象
			uc = (HttpURLConnection) realUrl.openConnection();
			uc.setDoOutput(true);
			uc.setDoInput(true);// 此属性默认值为true
			uc.setRequestMethod("POST");
			//POST请求不能使用缓存
			uc.setUseCaches(false);
			// 连接
			uc.connect();
			// post请求时需要此法写入请求参数，因为参数要放在http正文内，GET请求课直接将参数拼接在链接后面
			pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream(),"UTF-8"));
			pw.write(paramsString);
			pw.flush();// 需要flush，否则接口无法获取到参数
			pw.close();
			BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
			log.info("http请求返回结果："+result);
			in.close();
			uc.disconnect();
			log.info("成功调用Http工具");
			return result;
		} catch (Exception e) {
			log.error("发送Http请求异常:"+ e.getMessage());
			throw new Exception("发送Http请求异常");
		} 
	}
	
	public static void main(String[] args) throws Exception {
		String url = "http://shoudan.freemypay.com:18090/fmposm/login.ct";
		sendGetOrPost(url,"");
		
	}

}
